import Vue from 'vue';
import moment from 'moment';
import 'moment/locale/zh-cn';

Vue.filter('formatTime', (value, format) => {
  return value ? moment(value - 0).format(format || 'YYYY-MM-DD H:mm:ss') : '';
});

Vue.filter('formatPastTime', value => {
  if (!value) {
    return;
  }
  const date = new Date(value);
  const [dateYear, dateMonth, dateDays, dateHour, dateMin, dateSec] = [
    date.getFullYear(),
    date.getMonth() + 1,
    date.getDate(),
    date.getHours(),
    date.getMinutes(),
    date.getSeconds()
  ];
  let time = `${dateYear}-${dateMonth}-${dateDays} ${dateHour}:${dateMin}:${dateSec}`;
  return moment(time, 'YYYY-MM-DD H:mm:ss').fromNow();
});

// 11月05日 => 11 月 05 日
Vue.filter('formatMonthAndDay', value => {
  if (!value) {
    return;
  }
  let val = value.split('月');
  let day = val[1].split('日');
  return `${val[0]} 月 ${day[0]} 日`;
});

// 1时 => 01时
Vue.filter('formatHand', value => {
  if (value != 0 && !value) {
    return;
  }
  if (value.toString().length == 1) {
    value = '0' + value;
  }
  return value;
});
